package com.wohuijituan.whcsmerchant.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.wohuijituan.whcsmerchant.R;
import com.wohuijituan.whcsmerchant.adapter.IncomeRecordAdapter;
import com.wohuijituan.whcsmerchant.bean.IncomeListBean;
import com.wohuijituan.whcsmerchant.callback.IncomeListCallBack;
import com.wohuijituan.whcsmerchant.url.ParamsKey;
import com.wohuijituan.whcsmerchant.url.Urls;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;

/**
 * Created by zsy_18 on 2016/12/27.
 */

public class IncomeRecordActivity extends MyBaseActivity implements View.OnClickListener{
    private PullToRefreshListView lvPullIncome;
    private ImageButton titleGoBack;
    private TextView titleName;
    private TextView titleTvAction;
    private ImageButton titleIbAction;
    private List<IncomeListBean.DataBeanX.DataBean> incomeList;

    private void assignViews() {
        titleGoBack = (ImageButton) findViewById(R.id.title_go_back);
        titleName = (TextView) findViewById(R.id.title_name);
        titleTvAction = (TextView) findViewById(R.id.title_tv_action);
        titleIbAction = (ImageButton) findViewById(R.id.title_ib_action);
        lvPullIncome = (PullToRefreshListView) findViewById(R.id.pull_income_record);
    }
    private IncomeRecordAdapter incomeAdapter;
    private String[] strings = {"fsd","fsd","fsd","fsd","fsd","fsd","fsd","fsd","fsd","fsd","fsd"};
    private int nowPage=1,totalPage=1;
    private final int rows = 20;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_income_record);
        assignViews();
        initData();
        initView();
        initListener();
        incomeListRequest(1,rows);
    }

    private void initData() {
        incomeList = new ArrayList<>();
        incomeAdapter = new IncomeRecordAdapter(ctx, incomeList);
        lvPullIncome.setAdapter(incomeAdapter);
    }

    private void initView() {
        titleGoBack.setVisibility(View.VISIBLE);
        titleName.setText("收支明细");
        lvPullIncome.setMode(PullToRefreshBase.Mode.BOTH);//设置上拉下拉
    }

    private void initListener() {
        titleGoBack.setOnClickListener(this);
        lvPullIncome.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                incomeListRequest(1,rows);
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                if (nowPage<totalPage){
                    incomeListRequest(nowPage+1,rows);
                }else {
                    handler.postDelayed(new Runnable() {//如果当前页数为最后一页,则进行刷新停止
                        @Override
                        public void run() {
                            showToast(ctx,"没有更多数据!");
                            lvPullIncome.onRefreshComplete();
                        }
                    },1000);
                }

            }
        });
    }

    /**
     * 获取用户的收入列表
     * @param pageIndex 当前页数
     * @param rows 行数
     */
    private void incomeListRequest(int pageIndex,int rows){
        String strUrl = Urls.GetIncomeList;
        params.clear();
        params.addStringRequest(ParamsKey.IncomeList_UserKey,myApplication.getUserKey());
        params.addStringRequest(ParamsKey.IncomeList_PageIndex,pageIndex+"");
        params.addStringRequest(ParamsKey.IncomeList_Rows,rows+"");
        myOkHttpUtils.postRequest(strUrl, params, new IncomeListCallBack() {
            @Override
            public void onError(Call call, Exception e, int id) {
                showToast(IncomeRecordActivity.this,"网络错误!");
                lvPullIncome.onRefreshComplete();
            }

            @Override
            public void onResponse(IncomeListBean response, int id) {
                Message msg = new Message();
                msg.what = INCOME_LIST_REQUEST_OK;
                msg.obj = response;
                handler.sendMessage(msg);
            }
        });
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.title_go_back:
                onBackPressed();
                break;
        }
    }
    private static final int INCOME_LIST_REQUEST_OK = 11306;
    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case INCOME_LIST_REQUEST_OK:
                    //  2017/1/9 收入列表请求成功
                    lvPullIncome.onRefreshComplete();//刷新完成
                    IncomeListBean incomeListBean = (IncomeListBean) msg.obj;
                    if(incomeListBean.getState()==1){
                        totalPage = incomeListBean.getData().getPageCount();
                        nowPage = incomeListBean.getData().getPageIndex();
                        List<IncomeListBean.DataBeanX.DataBean> dataList = incomeListBean.getData().getData();
                        if (dataList.size()!=0){
                            if (nowPage ==1){
                                incomeAdapter.replace(dataList);
                            }else {
                                incomeAdapter.addAll(dataList);
                            }
                        }else {
                            showToast(ctx,incomeListBean.getMsg());
                        }
                    }else {
                        showToast(ctx,incomeListBean.getMsg());
                    }

                    break;
            }
        }
    };
}
